{% extends "repo/repo.html" %}

{% block subcontainer %}

<div class="issues inner bubble">
	<div class="sidebar">
		{% include "repo/condition.html" %}
	</div>

	<div class="primary issue-list">
    <div class="header">
      <a href="/{{user_name}}/{{repo_name}}/issues/create/" class="btn btn-success new-issue-btn">添加一个问题</a>
      <ul class="sortable clearfix">
        <li>{%if orderby == 'create_time'%}<a id="i_modify_time" href="javascript:void(0)" class="btn first">更新</a>{%else%}<span class="btn btn-disable first">更新</span>{%endif%}</li>
        <li>{%if orderby == 'modify_time'%}<a id="i_create_time" href="javascript:void(0)" class="btn last">创建</a>{%else%}<span class="btn btn-disable last">创建</span>{%endif%}</li>
      </ul>
    </div>
		{% for issue in issues %}
		<section class="issue">
      <div class="tracker">
        <span class="label issue-id">#{{issue.id}}</span>
        <span class="label status {%if issue.status == "新建"%}open{%elif issue.status == "已指派"%}assigned{%elif issue.status == "进行中"%}processing{%elif issue.status == "已解决"%}resolved{%elif issue.status == "已关闭"%}closed{%elif issue.status == "已拒绝"%}rejected{%endif%}"><i class="icon-{%if issue.status == "新建"%}bookmark-empty{%elif issue.status == "已指派"%}resize-horizontal{%elif issue.status == "进行中"%}spinner{%elif issue.status == "已解决"%}ok{%elif issue.status == "已关闭"%}remove{%elif issue.status == "已拒绝"%}trash{%endif%}"></i>{{issue.status}}</span>
      </div>
			<div class="summary">
        <div class="title">
          <h3 class="heading">
            <span class="label {%if issue.tracker == "缺陷"%}defect{%elif issue.tracker == "功能"%}feature{%elif issue.tracker == "支持"%}support{%endif%}">{{issue.tracker}}</span>
            <a href="/{{user_name}}/{{repo_name}}/issues/{{issue.id}}/" class="txt">{{issue.subject|truncatechars:100}}</a>
          </h3>
          <span class="issue-id">#{{issue.id}}</span>
        </div>
				<div class="meta">
					<a href="/{{ user.username }}/">{{ user.username }}</a>
          <time class="unixtime hide date" pubdate="pubdate">{%if orderby == 'modify_time'%}{{issue.modify_time}}{%else%}{{issue.create_time}}{%endif%}</time>
          {% comment %}
          {% if user.id == repo.user_id %}
              <a href="javascript: void(0)" rel="tooltip" data-original-title="拖动到上面做属性更改">拖动</a>
          {% else %}
              <a href="javascript: void(0)" rel="tooltip"><i class="icon-ellipsis-vertical"></i></a>
          {% endif %}
          {% endcomment %}
				</div>
			</div>
		</section>
		{% endfor %}
		{% if not issues %}
			<div class="issue"><p class="alert alert-error"><img src="/static/img/icons/alert-triangle-yellow.png" alt="">没有问题，或者没有查看问题的权限，半公开和私有项目需要申请成为成员才能查看.</p></div>
		{% endif %}

    <ul class="pagination">
      {% if hasPre %}
      <li><a href="/{{user_name}}/{{repo_name}}/issues/"><i class="icon-home"></i>首页</a></li>
      <li><a id="i_pre_page" href="javascript:void(0)">前一页</a></li>
      {% endif %}
      {% if hasPre or hasNext %}
      <li class="active">第{{page}}页</li>
      {% endif %}
      {% if hasNext %}
      <li><a id="i_next_page" href="javascript:void(0)">后一页</a></li>
      {% endif %}
    </ul>
	</div>

</div>

{% endblock %}

{% block subjs %}
    <script src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.21/jquery-ui.min.js"></script>
    <script>
        $(function(){
            var current_attrs = {{current_attrs|safe}};
            $('#i_index_page').click(function(){ 
                    current_attrs['page'] = 0;
                    location_cons()
            });
            $('#i_pre_page').click(function(){ 
                    current_attrs['page'] = current_attrs['page'] - 1;
                    location_cons()
            });
            $('#i_next_page').click(function(){ 
                    current_attrs['page'] = current_attrs['page'] + 1;
                    location_cons()
            });
            $('#i_create_time').click(function(){ 
                    current_attrs['orderby'] = 'create_time';
                    location_cons()
            });
            $('#i_modify_time').click(function(){ 
                    current_attrs['orderby'] = 'modify_time';
                    location_cons()
            });
            $('.issues_attr').click(function(){ 
                    var keyValue = $(this).attr('value').split("___")
                    if(keyValue[0] == 'assigned' && keyValue[1] == '所有') {
                            keyValue[1] = '0';
                    }
                    current_attrs[keyValue[0]] = keyValue[1];
                    location_cons()
            });
            location_cons = function() {
                    window.location = "/{{user_name}}/{{repo_name}}/issues/list/"+ current_attrs['assigned'] +"/"+ current_attrs['tracker'] +"/"+ current_attrs['status'] +"/"+ current_attrs['priority'] +"/"+ current_attrs['orderby'] +"/"+ current_attrs['page'] +"/";
            }
            $('.c_unixtime').each(function(index){ 
                    $(this).html(moment(new Date($(this).html()*1000)).fromNow());
                    $(this).show();
            });
            $('.c_issue').tooltip({
                selector: "a[rel=tooltip]"
            })
            $('.c_drag_setting').draggable({
                    cursor: 'move',
                    revert: true
            });
            $('.issues_attr').droppable({
                    accept: '.c_drag_setting',
                    hoverClass: 'issues_attr_hovered',
                    drop: handleIssueDrop
            });
            function handleIssueDrop(event, ui){
                    var issue_id = ui.draggable.attr('value');
                    var issue_attr = $(this).attr('value');
                    $.ajax({
                            url: '/{{user_name}}/{{repo_name}}/issues/update/' + issue_id + '/' + issue_attr + '/',
                            type: 'POST',
                            data: {csrfmiddlewaretoken: '{{ csrf_token }}'},
                            dataType: 'json',
                            timeout: 10000,
                            error: function(){
                            },
                            success: function(json){
                                    location_cons()
                            },
                    });
            }
        });
    </script>
{% endblock %}
